using System;
using System.Linq;
using System.Collections.Generic;
using T.FAS.Runtime.DataModel.Plugin;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.DataAccess.Common;
using Newtonsoft.Json;
using T.FAS.Runtime.DataAccess.SQLDom;

namespace T.FAS.Runtime.DataAccess.Engine
{
    /// <summary>
    /// 权限校验工具类
    /// </summary>
    public class AuthTool
    {
        internal static List<ColumnPermissionView> ColumnPermissionViews = null;

        /// <summary>
        /// sys权限sdk
        /// </summary>
        //private static readonly IDataPermissionsService s_permissionsService = DataPermissionsService.GetInstance();

        /// <summary>
        /// 判断是否需要走鉴权逻辑，同时获取DQ可用列权限结果
        /// </summary>
        /// <param name="dq"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool IsNeedAuthAndTryGetAuthResult(DataQuery dq, out List<ColumnPermissionView> result)
        {
            result = null;
            List<DataObject> dataObjects = new List<DataObject>();
            foreach (var source in dq.DataQuerySources)
            {
                if (source.DataObject != null && source.DataObject.IsHaveColumnPermission())
                    dataObjects.Add(source.DataObject);
            }
            //dq的全局鉴权开关，暂时不起作用
            //if (dq == null || !dq.IsEnableMask) return false;
            if (dataObjects.Count > 0)
            {
                //cdo暂时没有全局鉴权开关
                result = GetDataColumnAuthResult(dataObjects.Select(o => o.ID).ToList());
                return true;
            }
            return false;
        }

        /// <summary>
        /// 判断是否需要走鉴权逻辑，同时获取cdo可用列权限结果
        /// </summary>
        /// <param name="cdo"></param>
        /// <returns></returns>
        public static bool IsNeedAuthAndTryGetAuthResult(CompositeDataObject cdo, out List<ColumnPermissionView> result)
        {
            result = null;
            if (LiveDebug.IsDebug)
            {
                if (LiveDebug.IsAllAuth)
                    return true;
                if (LiveDebug.IsNoAuth)
                    return false;
            }
            List<DataObject> dataObjects = new List<DataObject>();
            foreach (var node in cdo.CompositeNodeObjects)
            {
                if (node.Associations?.Count > 0)
                {
                    foreach (var asso in node.Associations)
                    {
                        if (asso.AssociateType == AssociateType.OuterLeftJoin)
                        {
                            if (asso.AssoDataObject != null && asso.AssoDataObject.IsHaveColumnPermission())
                                dataObjects.Add(asso.AssoDataObject);
                        }
                    }
                }
                if (node.DataObject.IsHaveColumnPermission())
                    dataObjects.Add(node.DataObject);
            }
            if (dataObjects.Count > 0)
            {
                //cdo暂时没有全局鉴权开关
                result = GetDataColumnAuthResult(dataObjects.Select(o => o.ID).ToList());
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取可用列权限结果
        /// </summary>
        /// <param name="doIds"></param>
        /// <returns></returns>
        private static List<ColumnPermissionView> GetDataColumnAuthResult(List<string> doIds)
        {
            if (LiveDebug.IsDebug)
            {
                if (LiveDebug.IsAllAuth)
                    return ColumnPermissionViews;
                if (LiveDebug.IsNoAuth)
                    return null;
            }

            if (doIds?.Count > 0)
            {
                /* Todo
                //获取低代码列权限控制授权结果sql
                //返回SQL如下，注意处理with(nolock)： ((exists (select Data from SYS_DataAUDetail with(nolock) where DeleteMark=0 and DataAUResultID = '50336276-ad4d-4e3d-a73b-2e9030720487' and Data=v_fas_column_permission.ID) ) or ( 1 = 1)) 
                var sql = s_permissionsService.GetFilterSqlByUserId(UserUtil.GetCurrentUserID(), DACConst.FASColumnPermissionFuncID);
                sql = sql.Replace("with(nolock)", "");                
                return DataModelRTPClient.Instance.GetColumnPermissionViews(doIds, sql);
                */
            }
            return null;
        } 
    }
}
